package com.gitee.deeround.jdbchelper.core.impl;

import com.gitee.deeround.jdbchelper.core.DbActionDdl;
import com.gitee.deeround.jdbchelper.core.DbActionQuery;
import com.gitee.deeround.jdbchelper.core.Dialect;
import com.gitee.deeround.jdbchelper.core.DialectHelper;
import com.gitee.deeround.jdbchelper.exception.DbException;
import com.gitee.deeround.jdbchelper.out.DbColumn;
import com.gitee.deeround.jdbchelper.out.DbTable;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.Statement;
import java.util.List;

public class DbActionDdlImpl extends BaseDbAction implements DbActionDdl {

    private final JdbcTemplate jdbcTemplate;
    private final DbActionQuery dbActionQuery;

    public DbActionDdlImpl(JdbcTemplate jdbcTemplate, DbActionQuery dbActionQuery) {
        this.jdbcTemplate = jdbcTemplate;
        this.dbActionQuery = dbActionQuery;
    }

    private void excuteDdl(List<String> sqls) {
        try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {
            connection.setAutoCommit(true); // DDL默认自动提交，此处显式设置
            try (Statement statement = connection.createStatement()) {
                for (String sql : sqls) {
                    try {
                        statement.execute(sql);
                    } catch (Throwable ex) {
                        throw new DbException("执行DDL语句[" + sql + "]异常：" + ex.getMessage(), ex);
                    }
                }
            }
        } catch (Throwable ex) {
            // 处理DDL执行失败的情况
            throw new DbException("执行DDL语句异常：" + ex.getMessage(), ex);
        }
    }

    @Override
    public boolean existsTable(String tableName) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        String sql = dialect.existsTable(tableName);
        return dbActionQuery.queryForInteger(sql) > 0;
    }

    @Override
    public boolean existsTableColumn(String tableName, String columnName) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        String sql = dialect.existsTableColumn(tableName, columnName);
        return dbActionQuery.queryForInteger(sql) > 0;
    }

    @Override
    public void createTable(DbTable dbTable) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        List<String> sqls = dialect.createTable(dbTable);
        excuteDdl(sqls);
    }

    @Override
    public void dropTable(String tableName) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        List<String> sqls = dialect.dropTable(tableName);
        excuteDdl(sqls);
    }

    @Override
    public void copyTable(String tableName, String newTableName) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        List<String> sqls = dialect.copyTable(tableName, newTableName);
        excuteDdl(sqls);
    }

    @Override
    public void addTableColumn(DbColumn dbColumn) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        List<String> sqls = dialect.addTableColumn(dbColumn);
        excuteDdl(sqls);
    }

    @Override
    public void modifyTableColumn(DbColumn dbColumn) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        List<String> sqls = dialect.modifyTableColumn(dbColumn);
        excuteDdl(sqls);
    }

    @Override
    public void removeTableColumn(String tableName, String columnName) {
        Dialect dialect = DialectHelper.getDialect(jdbcTemplate);
        List<String> sqls = dialect.removeTableColumn(tableName, columnName);
        excuteDdl(sqls);
    }

}
